<template>
  <div class="topbar">
    <div class="wrap wTopbar">
      <div class="l">欢迎来到叩丁狼积分商城</div>
      <ul class="r">
        <li>
          <img :src="userInfo.headImg" alt="" class="avator" /><span>
            用户名：{{ userInfo.nickName }}</span
          >
        </li>
        <li>我的积分: {{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" width="20" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeixinLoginAPI } from "@/request/api";
export default {
  created() {
    // 只要用户扫码了,页面会重新刷新,此时可以获取code值
    //[!!!有可能会获取不到code...$nextTick()!!!!]
    // console.log("this.$route", this.$route.query.code);
    // console.log("topbar的create的钩子函数执行了。********");
    this.$nextTick(async () => {
      let mycode = this.$route.query.code;

      // if (!mycode) return;
      // 重载TopBar
      if (!mycode) {
        let mytoken = localStorage.getItem("x-auth-token");
        this.changeIsLogin(Boolean(mytoken));
        if (mytoken) {
          // console.log("更新用户信息。。。。");
          this.asyncChangeUserInfo();
        } else {
          // console.log("执行了初始化");
          this.initUserInfo();
        }
        return;
      }

      // 当有code时,就可以发起微信扫码请求
      let res = await WeixinLoginAPI({
        code: mycode,
      });
      if (res.code == 0) {
        alert("登录成功");
        // 存储token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 购物车显示
        this.changeIsLogin(true);
        // 扫码显示用户信息
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        // 二维码失效
        alert("请重新扫描二维码");
        // 登录框显示
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        // 第一次进来直接用微信扫码登录 用户没有绑定手机号  重新打开登录框
        // 当code为407时  res多一个uuid
        alert("请使用手机验证登录一次");
        this.changeIsShowLoginModal(true);
        localStorage.setItem("uuid", res.uuid);
        // console.log("WeixinLoginAPI", res);

        // 回到第一次进来 直接微信扫码的场景
        //http://codesohigh.com/store-pc/home  点头像  解绑
      }
    });
  },
  data() {
    return {};
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogin: "loginStatus/changeIsLogin",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  // 监听路由
  // 通过监听路由方式  切换登录状态  (方式一)
  // 希望切换路由时  让TopBar重载
  // 登录后  删除token  点击其他路由 购物车隐藏
  /*watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        console.log("$route.path", newVal, oldVal);
        let mytoken = localStorage.getItem("x-auth-token");
        // if (mytoken) {
        // console.log("登录状态为true");
        // this.changeIsLogin(true);
        // } else {
        // console.log("未登录状态为false");
        // this.changeIsLogin(false);
        // }
        this.changeIsLogin(Boolean(mytoken));
      },
    },
  },*/
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  line-height: 40px;
  .wTopbar {
    display: flex;
    justify-content: space-between;
    .r {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        text-align: center;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .avator {
        width: 22px;
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>
